package cn.yhjz.platform.system.controller;

import cn.yhjz.common.AjaxResponse;
import cn.yhjz.common.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionController {

    //返回json格式的响应结果
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public AjaxResponse exceptionHandler(Exception e, HttpServletResponse response) {
        log.error("{}", e);
        AjaxResponse resp = new AjaxResponse();
        resp.setCode(500);
        resp.setErrMsg("系统错误，请联系管理员");
        return resp;
    }

    //返回json格式的响应结果
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public AjaxResponse serviceExceptionHandler(ServiceException e, HttpServletResponse response) {
        AjaxResponse resp = new AjaxResponse();
        resp.setCode(500);
        resp.setErrMsg(e.getMessage());
        return resp;
    }
}
